﻿using System;
using System.Linq;
using System.Net.NetworkInformation;

namespace TcpServer
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.Title = "TcpServer";

            string testIp = "127.0.0.1";

            TcpLibrary.TcpServer tcpServer = new TcpLibrary.TcpServer(testIp, 4444);

            tcpServer.Start();

            while (true)
            {
                System.Threading.Thread.Sleep(3000);

                IPGlobalProperties ipProperties = IPGlobalProperties.GetIPGlobalProperties();

                var tcpResult = ipProperties.GetActiveTcpConnections();

                var tcpConnections = tcpResult.Where(t => t.LocalEndPoint.Port == 4444 || t.RemoteEndPoint.Port == 4444);

                if (tcpConnections.Count() > 0)
                {
                    foreach (var tcpConnection in tcpConnections)
                    {
                        Console.WriteLine($"local:{tcpConnection.LocalEndPoint} remote:{tcpConnection.RemoteEndPoint} state:{tcpConnection.State} {DateTime.Now.ToString("hh:MM:ss")}");
                    }
                }
                else
                {
                    Console.WriteLine($"连接不存在 {DateTime.Now.ToString("hh:MM:ss")}");
                }
            }
        }
    }
}
